import {
    Component,
    ElementRef,
    EventEmitter,
    Injector,
    Input,
    OnChanges,
    OnInit,
    Output,
    SimpleChanges
} from '@angular/core';
import { ConfigModule } from 'src/app/config.module';
import { NzMessageService } from 'ng-zorro-antd/message';
import { listAnimation } from '../../../animations/list.animation';
import {
    EventKeyEnum,
    EventPublishDTO,
    OptionTypeEnum,
    TableOptionModel
} from '../../table/table-option/table-option.model';

@Component({
    selector: 'app-sub-table',
    templateUrl: './sub-table.component.html',
    styleUrls: ['./sub-table.component.scss'],
    animations: [listAnimation]
})
export class SubTableComponent implements OnInit, OnChanges {

    @Input()
    searchObj = {
        cateId: '',
        pageNum: 1,
        pageSize: 1000
    };
    @Input()
    tableData: any[];
    @Input()
    dataService: any;
    @Input()
    mainItem: any;
    @Input()
    cateId: string;
    @Input()
    expand: boolean;
    @Input()
    refresh: string;
    @Input()
    tableKey = 'id';
    @Input()
    tableColumnMap = [];
    @Input()
    tableCaseMap: Map<any, Map<string, any>>;
    total: number;
    tableLoading = false;
    scrollValue = { x: '', y: '' };
    //组件显示
    @Input()
    optionModelList:TableOptionModel[] = null as TableOptionModel[];
    @Input()
    otherOptionModelList: TableOptionModel[] = null as TableOptionModel[];

    constructor(
        private _injector: Injector,
        public _config: ConfigModule,
        private msg: NzMessageService,
        private el: ElementRef
    ) {
    }

    ngOnChanges(changes: SimpleChanges): void {
        if (changes.hasOwnProperty('expand')){
            if (this.expand === true){
                this.list();
            }
        }
        if (changes.hasOwnProperty('refresh')){
                this.list();
        }
    }

    list(){
        this.tableLoading = true;
        this.searchObj.cateId = this.cateId;
        this.dataService.list(this.searchObj).subscribe(
            res => {
                if (res.success){
                    this.tableData = res.rows;
                    this.total = res.total;
                }
            }
        ).add( () => {
            this.tableLoading = false;
        });
    }

    ngOnInit(): void {
    }
    @Output()
    optionEvent = new EventEmitter();

    protected readonly OptionTypeEnum = OptionTypeEnum;
    protected readonly OptionKeyEnum = EventKeyEnum;

    //触发事件
    eventPublish(item: any, eventKey: EventKeyEnum) {
        let data = {
            eventKey: eventKey,
            publishData: item
        } as EventPublishDTO;
        this.optionEvent.emit(data);
    }

    onDelete(item){
        this.dataService.delete(item[this.tableKey]).subscribe(res=>{
            if (res.code == 200){
                this.list()
            }
        })
    }

    //选中按钮
    checkedButtonChange(item:any) {
        if (item.isCheckedButton){
            let data = {
                eventKey: this.OptionKeyEnum.SELECT,
                publishData: item
            } as EventPublishDTO;
            this.optionEvent.emit(data);
        }else {
            let data = {
                eventKey: this.OptionKeyEnum.UNSELECT,
                publishData: item
            } as EventPublishDTO;
            this.optionEvent.emit(data);
        }
    }

    add() {
        let data = {
            eventKey: EventKeyEnum.ADD,
            publishData: null
        } as EventPublishDTO;
        this.optionEvent.emit(data);
    }


    protected readonly EventKeyEnum = EventKeyEnum;
}
